package com.manage.util;

import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.jpush.api.JPushClient;
import cn.jpush.api.common.resp.APIConnectionException;
import cn.jpush.api.common.resp.APIRequestException;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Message;
import cn.jpush.api.push.model.Options;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.audience.AudienceTarget;
import cn.jpush.api.push.model.notification.AndroidNotification;
import cn.jpush.api.push.model.notification.IosNotification;
import cn.jpush.api.push.model.notification.Notification;

// 极光推送接口
public class PushUtil {

	public static final String appKey = "960acc15814819126baa9f6e";
	public static final String masterSecret = "27c56adaef0b938cdc99f378";

	public static final String testAppKey = "c2b7570a45bcc6560c74fc03";
	public static final String testMasterKey = "c0033bfbd72b97dc0d794926";

	protected static final Logger LOG = LoggerFactory.getLogger(PushUtil.class);

	public static JPushClient jpushClient = null;

	public static void main(String[] args) {
		testSendPush();
	}

	public static void testSendPush() {
		try {
			PushResult result = getClient()
					.sendPush(buildPushObject_android_and_ios("新活动来了！", new HashMap<String, String>()));
			System.out.println(result.isResultOK());
			LOG.info("Got result - " + result);
		} catch (APIConnectionException e) {
			LOG.error("Connection error. Should retry later. ", e);
		} catch (APIRequestException e) {
			LOG.error("Error response from JPush server. Should review and fix it. ", e);
			LOG.info("HTTP Status: " + e.getStatus());
			LOG.info("Error Code: " + e.getErrorCode());
			LOG.info("Error Message: " + e.getErrorMessage());
			LOG.info("Msg ID: " + e.getMsgId());
		}
	}

	public static boolean testSendPush(Map<String, String> map) {
		try {
			PushResult result = getClient()
					.sendPush(buildPushObject_android_and_ios("新活动来了！", new HashMap<String, String>()));
			LOG.info("Got result - " + result);
			return result.isResultOK();
		} catch (APIConnectionException e) {
			LOG.error("Connection error. Should retry later. ", e);
		} catch (APIRequestException e) {
			LOG.error("Error response from JPush server. Should review and fix it. ", e);
			LOG.info("HTTP Status: " + e.getStatus());
			LOG.info("Error Code: " + e.getErrorCode());
			LOG.info("Error Message: " + e.getErrorMessage());
			LOG.info("Msg ID: " + e.getMsgId());
		}
		return false;
	}

	public static JPushClient getClient() {
		if (jpushClient == null) {
			synchronized (PushUtil.class) {
				if (jpushClient == null) {
					String mk = SystemUtil.getProp("jpush_masterSecret");
					String ak = SystemUtil.getProp("jpush_appkey");
					System.out.println(mk.length());
					System.out.println(ak.length());
					jpushClient = new JPushClient(mk, ak);
				}
			}
		}
		return jpushClient;
	}

	public static PushPayload buildPushObject_android_and_ios(String title, Map<String, String> extras) {
		return PushPayload.newBuilder().setPlatform(Platform.android_ios()).setAudience(Audience.all())
				.setNotification(Notification.newBuilder().setAlert(title)
						.addPlatformNotification(
								AndroidNotification.newBuilder().setTitle("抚顺市图书馆").addExtras(extras).build())
						.addPlatformNotification(IosNotification.newBuilder().incrBadge(1).addExtras(extras).build())
						.build())
				.build();
	}

	public static PushPayload buildPushObject_all_all_alert(String ALERT) {
		return PushPayload.alertAll(ALERT);
	}

	public static PushPayload buildPushObject_all_alias_alert(String ALERT) {
		return PushPayload.newBuilder().setPlatform(Platform.all()).setAudience(Audience.all())
				.setNotification(Notification.alert(ALERT)).build();
	}

	public static PushPayload buildPushObject_android_tag_alertWithTitle(String ALERT, String TITLE,
			Map<String, String> extras) {
		return PushPayload.newBuilder().setPlatform(Platform.android()).setAudience(Audience.all())
				.setNotification(Notification.android(ALERT, TITLE, extras)).build();
	}

	public static PushPayload buildPushObject_android_and_ios() {
		return PushPayload.newBuilder().setPlatform(Platform.android_ios()).setAudience(Audience.tag("tag1"))
				.setNotification(Notification.newBuilder().setAlert("alert content")
						.addPlatformNotification(AndroidNotification.newBuilder().setTitle("Android Title").build())
						.addPlatformNotification(
								IosNotification.newBuilder().incrBadge(1).addExtra("extra_key", "extra_value").build())
						.build())
				.build();
	}

	public static PushPayload buildPushObject_ios_tagAnd_alertWithExtrasAndMessage(String ALERT, String MSG_CONTENT) {
		return PushPayload.newBuilder().setPlatform(Platform.ios()).setAudience(Audience.tag_and("tag1", "tag_all"))
				.setNotification(Notification.newBuilder()
						.addPlatformNotification(IosNotification.newBuilder().setAlert(ALERT).setBadge(5)
								.setSound("happy").addExtra("from", "JPush").build())
						.build())
				.setMessage(Message.content(MSG_CONTENT))
				.setOptions(Options.newBuilder().setApnsProduction(true).build()).build();
	}

	public static PushPayload buildPushObject_ios_audienceMore_messageWithExtras(String MSG_CONTENT) {
		return PushPayload.newBuilder().setPlatform(Platform.android_ios())
				.setAudience(Audience.newBuilder().addAudienceTarget(AudienceTarget.tag("tag1", "tag2"))
						.addAudienceTarget(AudienceTarget.alias("alias1", "alias2")).build())
				.setMessage(Message.newBuilder().setMsgContent(MSG_CONTENT).addExtra("from", "JPush").build()).build();
	}
}
